﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using System.Data;
using System.IO;
using LDSP.Util;

namespace LDSP.UI.Admin
{
    public partial class AddAccidentCase : BRL.BaseSysAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    this.CheckQS();
                }
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitDataSelect();
                    }
                    this.InitDataDynamicAfter();
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected AccidentCase m_accidentCase = null;
        protected int LastID
        {
            get { return this.GetVSInt("LastID"); }
            set
            {
                this.SetVS("LastID", value);
                m_accidentCase = null;
            }
        }
        protected AccidentCase AccidentCase
        {
            get
            {
                if (m_accidentCase == null)
                {
                    m_accidentCase = new AccidentCase();
                    if (LastID != 0)
                    {
                        m_accidentCase.GetProperties(LastID);
                    }
                }
                return m_accidentCase;
            }
        }
        protected bool IsAdd
        {
            get { return LastID == 0; }
        }

        protected void CheckQS()
        {
            LastID = this.GetInt(this.Request.QueryString["id"]);
            if (AccidentCase.ID == 0)
            {
                LastID = 0;
            }
        }
        protected bool RB()
        {
            return true;
        }
        protected void SetStatus()
        {
            this.lbl_op.Text = IsAdd ? "【添加】" : "【修改】";
            this.lbl_op.Style["color"] = IsAdd ? "red" : "blue";
            this.ClientComfirm(this.btn_ok, string.Format("您确定执行{0}操作吗？", IsAdd ? "添加" : "修改"));
        }
        protected void InitDataSelect()
        {
            this.txt_name.Text = AccidentCase.Name;
            this.txt_comment.Text = AccidentCase.Comment;
            this.txt_summary.Text = AccidentCase.Summary;
            this.txt_result.Text = AccidentCase.Result;
            this.txt_reference.Text = AccidentCase.Reference;
            this.lbt_file.Text = "";
            if (AccidentCase.FileName != "")
            {
                this.lbt_file.Text = AccidentCase.FileName;
            }

            this.BindDDLAccidentCaseType();
            this.SetStatus();
        }
        protected void InitDataDynamicBefore()
        {
        }
        protected void InitDataDynamicAfter()
        {
        }

        protected void BindDDLAccidentCaseType()
        {
            DataTable dt = new AccidentCaseType().GetAll();
            this.ddl_accidentCaseType.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.ddl_accidentCaseType.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["id"].ToString()));
            }
            try
            {
                this.ddl_accidentCaseType.SelectedIndex = 0;
                this.ddl_accidentCaseType.SelectedValue = AccidentCase.TypeID.ToString();
            }
            catch { }
        }


        protected void ValidateDataBefore()
        {
        }
        protected void ValidateDataAfter()
        {
            if (AccidentCase.Name == "")
            {
                throw new BYFW.BOL.CustomException("名称不能为空！");
            }
        }
        protected void SetData()
        {
            this.ValidateDataBefore();
            AccidentCase.Name = this.txt_name.Text.Trim();
            AccidentCase.Comment = this.txt_comment.Text;
            AccidentCase.CreateDate = DateTime.Now;
            AccidentCase.TypeID = this.GetInt(this.ddl_accidentCaseType.SelectedValue);
            AccidentCase.Summary = this.txt_summary.Text;
            AccidentCase.Result = this.txt_result.Text;
            AccidentCase.Reference = this.txt_reference.Text;
            if (this.fup_file.HasFile)
            {
                AccidentCase.CreateNewFile(this.fup_file.FileName);
                this.fup_file.SaveAs(AccidentCase.GetDownloadFileFullName());
                AccidentCase.IsConverted = false;
            }
            this.ValidateDataAfter();
        }
        protected void Add()
        {
            this.SetData();
            AccidentCase.Add();
            this.LastID = 0;
            this.JSAlert("添加成功！");
        }
        protected void Update()
        {
            string tmpName = AccidentCase.Name;

            this.SetData();

            if (tmpName != AccidentCase.Name)
            {
                new DownloadLog().UpdateByDocName(AccidentCase.ID, DocType.事故案例, AccidentCase.Name);
            }

            AccidentCase.Update();
            this.JSAlert("更新成功！");
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            try
            {
                this.LastID = 0;
                this.InitDataSelect();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                if (IsAdd)
                {
                    this.Add();
                }
                else
                {
                    this.Update();
                }
                if (!AccidentCase.IsConverted)
                {
                    CommonService.ConvertAccidentCasePDFFileToDB();
                }
                this.InitDataSelect();

            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        protected void lbt_file_Click(object sender, EventArgs e)
        {
            this.StartDownload(AccidentCase.ID, DocType.事故案例);
        }
    }
}
